﻿using Journeo_backend.Configuration;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Journeo_backend.Controllers
{
    /// <summary>
    /// 测试控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [AllowAnonymous]
    public class TestController : ControllerBase
    {
        private JwtHelper jwtHelper;

        /// <summary>
        /// 通过SecretKey cannot be null or empty.得到Jwt
        /// </summary>
        /// <param name="jwtHelper"></param>
        public TestController(JwtHelper jwtHelper)
        {
            this.jwtHelper = jwtHelper;
        }
        /// <summary>
        /// 测试返回值
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Response<string> GetTest()
        {
            try
            {
                return new Response<string>("测试返回值");
            } catch (Exception e)
            {
                return new Response<string>("error", Messages.ERROR ,e.Message);
            }
        }


        /// <summary>
        /// 获取JwtToken
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Response<string> GetJwtToken()
        {
            try
            {
                var token = jwtHelper.GetJwtToken();
                return new Response<string>(token);
            }
            catch (Exception e)
            {
                return new Response<string>("error", Messages.ERROR, e.Message);
            }
        }

        /// <summary>
        /// 可以在方法前面加Authorize
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public Response<string> GetByJwt()
        {
            try
            {
                return new Response<string>("Jwt测试成功！");
            }
            catch (Exception e)
            {
                return new Response<string>("error", Messages.ERROR, e.Message);
            }
        }

        /// <summary>
        /// 不需要Jwt
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpGet]
        public Response<string> NoJwtGet()
        {
            try
            {
                return new Response<string>("我不需要Jwt");
            }
            catch (Exception e)
            {
                return new Response<string>("error", Messages.ERROR, e.Message);
            }
        }
    }
}
